#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
int a[N];
struct Data {
  int sum, mx, left, right;
};
struct Node {
  Data data[2];
  int tag;
} tr[N << 2];
#define data(p, k) tr[p].data[k]
#define sum(p, k) tr[p].data[k].sum
#define mx(p, k) tr[p].data[k].mx
#define left(p, k) tr[p].data[k].left
#define right(p, k) tr[p].data[k].right
#define tag(p) tr[p].tag

void merge(Node a, Node b, Node& c) {
  rep(k, 0, 1) {
    c.data[k].sum = a.data[k].sum + b.data[k].sum;
    c.data[k].mx =
        max({a.data[k].mx, b.data[k].mx, a.data[k].right + b.data[k].left});
    c.data[k].left = a.data[k].left;
    if (!a.data[k ^ 1].sum) c.data[k].left = a.data[k].sum + b.data[k].left;
    c.data[k].right = b.data[k].right;
    if (!b.data[k ^ 1].sum) c.data[k].right = b.data[k].sum + a.data[k].right;
  }
}
void pushup(int p) { merge(tr[p << 1], tr[p << 1 | 1], tr[p]); }
void build(int p, int l, int r) {
  tr[p].tag = 0;
  if (l == r) {
    int k = a[l];
    tr[p].data[k] = Data{1, 1, 1, 1};
    tr[p].data[k ^ 1] = Data{0, 0, 0, 0};
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
void addtag(int d, int p, int l, int r) {
  if (d == 1) {
    tag(p) = 1;
    data(p, 0) = Data{r + 1 - l, r + 1 - l, r + 1 - l, r + 1 - l};
    data(p, 1) = Data{0, 0, 0, 0};
    return;
  }
  if (d == 2) {
    tag(p) = 2;
    data(p, 1) = Data{r + 1 - l, r + 1 - l, r + 1 - l, r + 1 - l};
    data(p, 0) = Data{0, 0, 0, 0};
    return;
  }
  if (d == 4) {
    if (!(tag(p) & 3)) {
      tag(p) ^= 4;
      swap(data(p, 0), data(p, 1));
    } else {
      addtag(3 ^ tag(p), p, l, r);
    }
  }
}
void pushdown(int p, int l, int r) {
  if (tag(p)) {
    int mid = (l + r) >> 1;
    addtag(tag(p), p << 1, l, mid);
    addtag(tag(p), p << 1 | 1, mid + 1, r);
    tag(p) = 0;
  }
}
void update(int L, int R, int d, int p, int l, int r) {
  if (L <= l && R >= r) {
    addtag(d, p, l, r);
    return;
  }
  pushdown(p, l, r);
  int mid = (l + r) >> 1;
  if (L <= mid) update(L, R, d, p << 1, l, mid);
  if (R > mid) update(L, R, d, p << 1 | 1, mid + 1, r);
  pushup(p);
}
Node query(int L, int R, int p, int l, int r) {
  if (L <= l && R >= r) return tr[p];
  pushdown(p, l, r);
  int mid = (l + r) >> 1;
  if (L <= mid && R > mid) {
    Node a = query(L, R, p << 1, l, mid);
    Node b = query(L, R, p << 1 | 1, mid + 1, r);
    Node c;
    merge(a, b, c);
    return c;
  } else if (L <= mid)
    return query(L, R, p << 1, l, mid);
  else
    return query(L, R, p << 1 | 1, mid + 1, r);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  build(1, 1, n);
  // rep(i, 1, n) cout << query(i, i, 1, 1, n).data[1].sum << ' ';
  // cout << endl;
  while (m--) {
    int op, l, r;
    cin >> op >> l >> r;
    l++, r++;
    if (op <= 2) update(l, r, 1 << op, 1, 1, n);
    if (op == 3) cout << query(l, r, 1, 1, n).data[1].sum << endl;
    if (op == 4) cout << query(l, r, 1, 1, n).data[1].mx << endl;
    // rep(i, 1, n) cout << query(i, i, 1, 1, n).data[1].sum << ' ';
    // cout << endl;
  }
  return 0;
}